<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function HAjax(prmes) {
            let xhr = new XMLHttpRequest()
            if (prmes.method.toUpperCase() === 'GET') {
                let getQurey = ''
                for (const key in prmes.data) {
                    getQurey += `${key}=${prmes.data[key]}&`
                }
                xhr.open(prmes.method, prmes.url + '?' + getQurey)
                xhr.send()
                console.log(2222, getQurey)
            } else {
                let str = ''
                for (const key in prmes.data) {
                    str += `${key}=${prmes.data[key]}&`
                }
                xhr.open(prmes.method, prmes.url)
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(str)
                console.log('333', str)
            }
            return new Promise((succeed, fail) => {
                xhr.onreadystatechange = function () {
                    // readyState == 4说明请求已完成
                    console.log(1)
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200 || xhr.status == 304) {
                           succeed(xhr.responseText)
                        } else {
                            fail()
                        }
                    }
                }
            })

        }

        let d = {
            method: 'post',
            url: 'http://localhost:5000/postdata',
            data: { a: '1', b: '2', c: 123123 }
        }
        let b = {
            method: 'get',
            url: 'http://localhost:5000/page',
            data: { a: '1', b: '2', c: 123123 }
        }
        HAjax(b)

        HAjax(d).then(res => {
            console.log(444,JSON.parse(res).data)
        })

    </script>
</body>

</html>